﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Editor.PageControls;
using Editor.Utilities;

namespace Editor
{
    public partial class AddHideButtonForm : AddFormBase, IDictSubject<string>
    {
        private PageHideButton hideButton;

        public AddHideButtonForm()
        {
            InitializeComponent();
        }

        public override PageControl CreatedControl
        {
            get
            {
                return hideButton;
            }
            set
            {
                hideButton = (PageHideButton)value;
            }
        }

        private void AddHideButtonForm_Load(object sender, EventArgs e)
        {
            if (EditorForm.PageControls != null && EditorForm.PageControls.Length != 0)
            {
                comboBox1.Items.AddRange(EditorForm.PageControls);
                comboBox1.SelectedIndex = 0;
            }

            hideButton = new PageHideButton();
            Dictionary = new SDictionary<string>();
            textBoxName.Text = EditorForm.GenerateName(hideButton.NamingPrefix);
            comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);
            comboBoxLanguage.SelectedIndex = 0;
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (textBoxText.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Yazı girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            string selectedLanguage = comboBoxLanguage.SelectedItem as string;
            
            hideButton.Name = textBoxName.Text.Trim();
            Dictionary[selectedLanguage] = textBoxText.Text;
            hideButton.Dictionary = Dictionary;
            //hideButton.Text = textBoxText.Text;
            hideButton.HideControl = (string)comboBox1.SelectedItem;
            hideButton.TextFontSize = 9.5f;
            hideButton.Visible = true;
            hideButton.UpdateInnerControl();
            EditorForm.RefreshCanvas();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            SDictionary<string> dict = Dictionary;
            textBoxText.Text = changeSubject(comboBoxLanguage.SelectedItem as string,
                comboBoxLanguage.SelectedIndex, textBoxText.Text, ref dict);
            Dictionary = dict;
        }

        public SDictionary<string> Dictionary { get; set; }
    }
}
